package com.gsbelarus.gedemin.salary.activity;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MenuItem;

import com.gsbelarus.gedemin.lib.sync.protocol.entity.SyncServiceStatus;
import com.gsbelarus.gedemin.lib.ui.activity.SyncActivityModel;
import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.fragment.PersonInfoPrefFragment;
import com.gsbelarus.gedemin.salary.util.GCMHelper;

public class PersonInfo extends SyncActivityModel {

    private PersonInfoPrefFragment fragment;

    /**
     * @return id View для отрисовки в активити
     */
    @Override
    public int getViewResource() {
        return R.layout.activity_fragment;
    }

    @Override
    public void onReceiveSyncStatus(SyncServiceStatus syncServiceStatus, SyncStatusDialog syncStatusDialog) {
        super.onReceiveSyncStatus(syncServiceStatus, syncStatusDialog);
        if (syncServiceStatus.getTypeOfStatus() == SyncServiceStatus.TypeOfStatus.START_SYNC)
            finish();
        syncStatusDialog.show(syncServiceStatus);
    }

    /**
     * вызывается в методе onCreate(Bundle)
     *
     * @param arguments bundle с агрументами
     */
    @Override
    public void onCreateActivity(Bundle arguments) {
        super.onCreateActivity(arguments);
        if (getSupportActionBar() != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        if (fragment != null)
            fragmentTransaction.remove(fragment);
        fragment = new PersonInfoPrefFragment();
        fragmentTransaction.add(R.id.frame_content, fragment);
        fragmentTransaction.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new GCMHelper(this).checkPlayServices(this);
    }
}
